﻿using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Basic.SuperMap;


namespace Ycjw.PipeLine.Map.Tools.Conditions.Statistics
{
    public partial class GJCZStatistics : ConditionPanel
    {
        private LayerWithDataListPanel LayerWithDataListPanel;
        public GJCZStatistics() : base()
        {
            // this.SetSize(300, 200);
            this.ConditionPanelName = "管经按材质分类统计";
            InitializeComponent();
        }
        private readonly string CZFIELDNAME = "材质";

        protected override void OnLoad(EventArgs e)
        {
            LayerWithDataListPanel = new LayerWithDataListPanel()
            {
                Dock = System.Windows.Forms.DockStyle.Top
            };
            Scene scene = this.Scene;
            if (scene == null) return;
            LayerWithDataListPanel.Init(scene);
            //LayerWithDataListPanel.LoadToVector();
            var types = new List<DatasetType>()
            {
                DatasetType.Line,
                DatasetType.Line3D,
                DatasetType.Network,
                DatasetType.Network3D
            };
            LayerWithDataListPanel.LoadTo(types, true);
            this.uiGroupBoxtjtc.Controls.Add(LayerWithDataListPanel);
        }

        public override object Do(DockPanel dockPanel = null)
        {
            DataTable dt = new DataTable();
            dt.TableName = ConditionPanelName;
            dt.Columns.Add("系列");
            dt = StatisticsByAll(dt);
            return dt;
        }

        public override bool ConditionValid()
        {
            if (this.LayerWithDataListPanel.SelectLayer3DDatasets.Count == 0)
                return false;
            return true;
        }

        private DataTable StatisticsByAll(DataTable dt)
        {
            if (this.uiRadioButtoncdtj.Checked)
            {
                List<Layer3DDataset> datasets = this.LayerWithDataListPanel.SelectLayer3DDatasets;
                List<Dictionary<string, double>> results = new List<Dictionary<string, double>>();
                foreach (Layer3DDataset layer3DDataset in datasets)
                {
                    var dataSetVector = layer3DDataset.Dataset as SuperMap.Data.DatasetVector;
                    if (dataSetVector.FieldInfos.IndexOf(CZFIELDNAME) < 0) continue;
                    DatasetVectorEx dvEx = new DatasetVectorEx(layer3DDataset.Dataset.Name);
                    dvEx.DatasetVector = dataSetVector;
                    var result = dvEx.UniqueValLength(CZFIELDNAME);

                    results.Add(result);

                    foreach (var key in result.Keys)
                    {
                        var tmpKey = key;
                        if (string.IsNullOrEmpty(key))
                            tmpKey = "未知";
                        if (!dt.Columns.Contains(tmpKey))
                            dt.Columns.Add(tmpKey);
                    }
                }
                foreach (Layer3DDataset layer3DDataset in datasets)
                {
                    var dr = dt.NewRow();
                    dr[0] = layer3DDataset.Caption;
                    dt.Rows.Add(dr);
                }
                var rowIndex = 0;
                foreach (var r in results)
                {
                    var dr = dt.Rows[rowIndex++];
                    foreach (var key in r.Keys)
                    {
                        var tmpKey = key;
                        if (string.IsNullOrEmpty(key))
                            tmpKey = "未知";
                        dr[tmpKey] = r[key];
                    }
                }
            }
            if (this.uiRadioButtontstj.Checked)
            {
                List<Layer3DDataset> datasets = this.LayerWithDataListPanel.SelectLayer3DDatasets;
                List<Dictionary<string, double>> results = new List<Dictionary<string, double>>();
                foreach (Layer3DDataset layer3DDataset in datasets)
                {
                    var dataSetVector = layer3DDataset.Dataset as SuperMap.Data.DatasetVector;
                    if (dataSetVector.FieldInfos.IndexOf(CZFIELDNAME) < 0) continue;
                    DatasetVectorEx dvEx = new DatasetVectorEx(layer3DDataset.Dataset.Name);
                    dvEx.DatasetVector = dataSetVector;

                    var result = dvEx.UniqueValLengths(CZFIELDNAME);

                    results.Add(result);

                    foreach (var key in result.Keys)
                    {
                        var tmpKey = key;
                        if (string.IsNullOrEmpty(key))
                            tmpKey = "未知";
                        if (!dt.Columns.Contains(tmpKey))
                            dt.Columns.Add(tmpKey);
                    }
                }
                foreach (Layer3DDataset layer3DDataset in datasets)
                {
                    var dr = dt.NewRow();
                    dr[0] = layer3DDataset.Caption;
                    dt.Rows.Add(dr);
                }
                var rowIndex = 0;
                foreach (var r in results)
                {
                    var dr = dt.Rows[rowIndex++];
                    foreach (var key in r.Keys)
                    {
                        var tmpKey = key;
                        if (string.IsNullOrEmpty(key))
                            tmpKey = "未知";
                        dr[tmpKey] = r[key];
                    }
                }
            }

            return dt;
        }
    }
}
